<?php

namespace App\Http\Controllers\admin\main;

use App\Http\Controllers\Data;
use App\Models\Count;
use App\Models\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class MainController extends Controller
{
    //管理员后台首页
    public function index()
    {
        $ob = new Data();
        $sum = $ob->select('d')->sum()->total();
        $order = $ob->select('d')->order()->total();
        $n = $this->user();
        $v=$this->visitor();
        return view('admin.main.main', ['sum' => $sum, 'order' => $order,'n'=>$n,'v'=>$v]);
    }
    /** 统计订单数据 */
    public function redata(Request $request)
    {
        $date = $request->input('a');
        $ob = new Data();
        $order=$ob->select($date)->order()->list();
        $oob=new Data();
        $count=$oob->select($date)->count()->list();
        $res=['order'=>$order,'count'=>$count];
        $res= json_encode($res);
        return $res;

    }
    /** 统计活跃用户 (一个月之内)*/
    public function user()
    {
        $ob = new User();
        $res = $ob->get();
        $n = '';
        foreach ($res as $v) {
            $time = $v->updated_at->getTimestamp();
            if (time() - $time <= 2592000) {
                $n++;
            }
        }
        return $n;
    }
    /** 统计访客数量 */
    public function visitor()
    {
        $d=date('d',time());
        $ob = new Count();
        $res =$ob->whereDay('created_at',$d)->count();
        return $res;
    }
}
